package com.my.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.model.admin.dtos.ChannelReqDto;
import com.my.model.common.dtos.PageRequestDto;
import com.my.model.common.dtos.PageResponseResult;
import com.my.model.common.dtos.ResponseResult;
import com.my.model.common.enums.AppHttpCodeEnum;
import com.my.model.wemedia.pojos.WmChannel;
import com.my.wemedia.mapper.WmChannelMapper;
import com.my.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.rankeval.RankEvalSpec;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Override
    public ResponseResult findAllChannel() {
        return ResponseResult.okResult(list());
    }

    /**
     * 管理端获取频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getChannel(ChannelReqDto dto) {
        if(dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        IPage<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(dto.getName()),WmChannel::getName,dto.getName());
        lqw.orderByDesc(WmChannel::getCreatedTime);
        IPage<WmChannel> iPage = page(page,lqw);

        return ResponseResult.okResult(iPage.getRecords());
    }

    /**
     * 保存频道信息
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        if(wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
